﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using H2.NET.Controls;
using H2.NET.Data.Common;
using H2.NET.Business;



namespace H2.NET.Web.Pages
{
    public partial class TabFormu : TaskControl
    {
        #region 属性 Public
        public MDCTabStatus Status
        {
            get
            {
                if (StoreAPI["Status~" + Key] == null)
                {
                    StoreAPI["Status~" + Key] = new MDCTabStatus();
                }
                return (MDCTabStatus)StoreAPI["Status~" + Key];
            }
            set
            {
                Key = value.KeyWord;
                StoreAPI["Status~" + Key] = value;
            }
        }

        public string Key
        {
            get
            {
                return ViewState["Key"] == null ? "" : (string)ViewState["Key"];

            }
            set { ViewState["Key"] = value; }
        }

        public int SheetIndex
        {
            get
            {
                return ViewState["SheetIndex"] == null ? 0 : (int)ViewState["SheetIndex"];

            }
            set { ViewState["SheetIndex"] = value; }
        }

         public bool IsHaveAttachment
        {
            get
            {
                return ViewState["IsHaveAttachment"] == null ? false : (bool)ViewState["IsHaveAttachment"];
            }
            set
            {
                ViewState["IsHaveAttachment"] = value;
            }
        }

        #endregion

        #region 事件 页面加载
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            //如果当前 Tabs 是隐藏的情况，退出
            if (!Status.IsUsed)
            {
                return;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            InitControls();
            base.OnLoad(e);
        }

        public void InitControls()
        {
            
            #region FORMU
            if (Status.Category == "FORMU")
            {
                bool flg1 = Status.Marketing == "C" && TaskAPI.IsAllowSubmit;
                bool flg2 = Status.DMO == "C" && TaskAPI.IsAllowSubmit;
                bool flg3 = Status.Technical == "C" && TaskAPI.IsAllowSubmit;
                gv_Formu.DynamicColumn.Add(new BoundColumn2("00", "No", BoundColumnType2.TextBox, 80, Align.Left, false, "No", InputType.None, null));
                gv_Formu.DynamicColumn.Add(new BoundColumn2("01", "SAP CODE</br>(创建FINI Code时Copy)</br>(创建FINI BOM时Copy)</br>(创建Barcode时Copy)", BoundColumnType2.TextBox, 400, Align.Left, flg1, "SAP_CODE", InputType.None, null));
                gv_Formu.DynamicColumn.Add(new BoundColumn2("02", "SKU BPCS Code", BoundColumnType2.TextBox, 300, Align.Left, flg1, "SKU_BPCS_Code", InputType.None, null));
                gv_Formu.DynamicColumn.Add(new BoundColumn2("03", "Description 1(EN.) SKU描述</br>（原英文描述后面加(YYMM)）", BoundColumnType2.TextBox, 400, Align.Left, flg1, "Description_EN", InputType.None, null));
                gv_Formu.DynamicColumn.Add(new BoundColumn2("04", "Description 2(ZH.) SKU描述</br>（原中文描述后面加(YYMM)）", BoundColumnType2.TextBox, 400, Align.Left, flg1, "Description_ZH", InputType.None, null));
                gv_Formu.DynamicColumn.Add(new BoundColumn2("05", "Easy Order entry Code", BoundColumnType2.TextBox, 350, Align.Left, flg1, "Easy_Order_entry_Code", InputType.None, null));
                gv_Formu.DynamicColumn.Add(new BoundColumn2("06", "Density", BoundColumnType2.TextBox, 100, Align.Left, flg3, "Density", InputType.None, null));
                gv_Formu.DynamicColumn.Add(new BoundColumn2("07", "New SAP Code</br>(Created by DMO)", BoundColumnType2.TextBox, 400, Align.Left, flg2, "New_SAP_Code", InputType.None, null));
                gv_Formu.Width = 1000;
                SetEnable(flg1, gv_Formu_Add);
                SetEnable(flg1, gv_Formu_Delete);
                //SetVisible(gv_Plg.Enabled && gv_Plg.Visible, gv_Plg_Add, gv_Plg_Delete);
                if (!Page.IsPostBack)
                {
                    Formu_P16.Visible = true;
                    gv_Formu.Key = Key;

                    #region 发起步骤
                    if (TaskAPI.IsInitiate)
                    {
                        List<mdc_Material_Formu> formu_s = new List<mdc_Material_Formu>();
                        for (int i = 0; i < 10; i++)
                        {
                            formu_s.Add(new mdc_Material_Formu());
                            formu_s[i].No = i + 1;
                        }
                        gv_Formu.DataSource = formu_s;
                    }
                    #endregion

                    #region 审批环节
                    else
                    {
                        IList<mdc_Material_Formu> formu_s = (mdc_Material_Formu.process == TaskAPI.Process & mdc_Material_Formu.incident == TaskAPI.Incident).List<mdc_Material_Formu>();
                        if (formu_s.Count == 0)
                        {
                            for (int i = 0; i < 12; i++)
                            {
                                formu_s.Add(new mdc_Material_Formu());
                                formu_s[i].No = i + 1;
                            }
                        }
                        gv_Formu.DataSource = formu_s;
                    
                    }
                    #endregion

                }
            }
            #endregion

        }
        #endregion

        #region 事件 Add(增加行)
        protected void Add_Click(object sender, EventArgs e)
        {
            switch (((Anthem.Button)sender).ID)
            {
                case "gv_Formu_Add":
                    List<mdc_Material_Formu> formu_s = (List<mdc_Material_Formu>)gv_Formu.DataSource;
                    mdc_Material_Formu formu = new mdc_Material_Formu();
                    //formu.No = formu_s.Count + 1;
                    formu_s.Insert(gv_Formu.ChangedRowIndex == -1 || gv_Formu.ChangedRowIndex >= formu_s.Count ? formu_s.Count : gv_Formu.ChangedRowIndex, formu);
                    //gv_Formu.DataSource = formu_s;
                    //gv_Formu.DataBind();
                    for (int i = 0; i < formu_s.Count; i++)
                    {
                        formu_s[i].No = (i + 1);
                    }
                    gv_Formu.DataSource = formu_s;
                    gv_Formu.DataBind();
                    break;
            }
        }
        #endregion

        #region 事件 Delete(删除行)
        protected void Delete_Click(object sender, EventArgs e)
        {
            switch (((Anthem.Button)sender).ID)
            {
                case "gv_Formu_Delete":
                    List<mdc_Material_Formu> formu_s = (List<mdc_Material_Formu>)gv_Formu.DataSource;
                    IList<mdc_Material_Formu> list = (mdc_Material_Formu.process == TaskAPI.Process & mdc_Material_Formu.incident == TaskAPI.Incident).List<mdc_Material_Formu>();
                    if (formu_s.Count == 1)
                    {
                        Anthem.Manager.AddScriptForClientSideEval("JSInfomation('至少保留最后一个 Sheet ！',false)");
                        return;
                    }
                    formu_s.RemoveAt(gv_Formu.ChangedRowIndex == -1 || gv_Formu.ChangedRowIndex >= formu_s.Count ? formu_s.Count - 1 : gv_Formu.ChangedRowIndex);
                    bool flag = false;
                    foreach (mdc_Material_Formu formu_1 in list)
                    {
                        if (string.IsNullOrEmpty(formu_1.SAP_CODE))
                        {
                            continue;
                        }
                        foreach (mdc_Material_Formu formu_2 in formu_s)
                        {

                            if (string.IsNullOrEmpty(formu_1.SAP_CODE))
                            {
                                continue;
                            }
                            if (formu_2.ID == formu_1.ID)
                            {
                                flag = true;
                                break;
                            }
                        }
                        if (!flag)
                        {
                            Storage.Delete(formu_1);
                        }
                        flag = false;
                    }

                    gv_Formu.DataSource = formu_s;
                    gv_Formu.DataBind();
                    break;
            }
        }
        #endregion

        #region 方法 Save(保存数据库)
        public void Save()
        {
            if (!Status.IsEdited)
            {
                return;
            }

            switch (Status.Category)
            {

                #region FORMU
                case "FORMU":
                    foreach (mdc_Material_Formu formu in (List<mdc_Material_Formu>)gv_Formu.DataSource)
                    {
                        //if (string.IsNullOrEmpty(pkg_fini.FINI_Code))
                        //{
                        //    continue;
                        //}
                        formu.Process = TaskAPI.Process;
                        formu.Incident = TaskAPI.Incident;
                        formu.UserName = UserAPI.UserName;
                        formu.Time = System.DateTime.Now;
                        formu.Status = 0;
                        Storage.Save(formu);
                    }
                    break;
                #endregion
            }
        }
        #endregion

        #region 方法 DataValidation(数据验证)
        public List<string> DataValidation()
        {
            //如果当前 Tabs 是隐藏的情况，退出
            if (!(Status.IsUsed && TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit))
            {
                return new List<string>();
            }

            List<string> vald_s = new List<string>();
            List<Regular> regs = new List<Regular>();
            foreach (mdc_Material_Formu formu in (List<mdc_Material_Formu>)gv_Formu.DataSource)
            {
                if (!string.IsNullOrEmpty(formu.SAP_CODE))
                {
                    regs.Add(new Regular(RegularExp.NotEmpty, formu.SKU_BPCS_Code, "&nbsp;&nbsp;SKU BPCS Code为必填"));
                    regs.Add(new Regular(RegularExp.NotEmpty, formu.Description_EN, "&nbsp;&nbsp;英文描述为必填"));
                    regs.Add(new Regular(RegularExp.NotEmpty, formu.Description_ZH, "&nbsp;&nbsp;中文描述为必填"));
                    if (TaskAPI.Step == "Technical")
                    {
                        regs.Add(new Regular(RegularExp.NotEmpty, formu.Density, "&nbsp;&nbsp;密度为必填"));
                    }
                    
                }
            }

            vald_s.AddRange(Validation(regs));

            if (vald_s.Count > 0)
            {
                vald_s.Insert(0, "<font color='#0000FF'><b>" + Status.Name + "</b></font>");
                vald_s.Add("&nbsp;&nbsp;-----------------------------------------");
            }

            return vald_s;
        }
        #endregion

    }
}